package com.my.helper.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.my.helper.R;
import com.my.helper.bean.RegisterRoot;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.SharedPrefUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private String registerUrl = "http://www.wanandroid.com/user/register";
    private EditText etUserName;
    private EditText etPassword;
    private EditText etPasswordAgan;
    private Button btnRegister;
    private String strUserName;
    private String strPassword;
    private String strPasswordAgain;
    private SharedPrefUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        CacheActivity.addActivity(this);
        sp = new SharedPrefUtil(this, "myHelper");

        initView();
        initListener();
    }

    private void initView() {
        etUserName = findViewById(R.id.et_user_name_register);
        etPassword = findViewById(R.id.et_user_password_register);
        etPasswordAgan = findViewById(R.id.et_user_password_agin_register);
        btnRegister = findViewById(R.id.btn_regeste);
        btnRegister.setOnClickListener(this);
    }

    private void initListener() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_regeste: // 注册
                checkRegester();
                break;
        }
    }

    /**
     * 检查注册信息
     */
    private void checkRegester() {
        strUserName = etUserName.getText().toString();
        strPassword = etPassword.getText().toString();
        strPasswordAgain = etPasswordAgan.getText().toString();

        if (strUserName.isEmpty()) {
            ToastUtil("请输入用户名");
        } else if (strPassword.isEmpty()) {
            ToastUtil("请输入密码");
        } else if (strPassword.equals(strPasswordAgain)) {
            goRegester();
        } else {
            ToastUtil("两次输入密码不一致请重新输入");
        }
    }

    /**
     * 跳转注册
     */
    private void goRegester() {
        OkHttpUtils
                .post()
                .url(registerUrl)
                .addParams("username", strUserName)
                .addParams("password", strPassword)
                .addParams("repassword", strPasswordAgain)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil("注册服务器开小差了");
                        Log.i("bmw", "注册请求失败：" + e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
//                        ToastUtil("登录成功");
                        Log.i("bmw", "注册请求成功：" + response);
                        Gson gson = new Gson();
                        String msgs = "注册失败";
                        int code;
                        try {
                            RegisterRoot root = gson.fromJson(response, RegisterRoot.class);
                            msgs = root.getErrorMsg();
                            code = root.getErrorCode();
                            if (code == 0) {
                                // 注册成功
                                CacheActivity.finishSingleActivityByClass(RegisterActivity.class);
                                ToastUtil("注册成功！");
                                sp.putString("user_name", strUserName);
                                sp.commit();
                            } else {
                                ToastUtil(msgs);
                            }

                        } catch (Exception e) {
                            Log.i("BMW", "注册解析失败");
                        }
                    }
                });
    }

    private void ToastUtil(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
